package com.ys.service.sys.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.sys.YsGridButton;
import com.ys.mapper.sys.SysMapper;
import com.ys.mapper.sys.YsGridButtonMapper;
import com.ys.service.sys.YsGridButtonService;
import com.ys.vo.R;

@Service("ysGridButtonService")
public class YsGridButtonServiceImpl implements YsGridButtonService {

	@Autowired
	private SysMapper sysMapper;
	@Autowired
	private YsGridButtonMapper ysGridButtonMapper;

	@Override
	@Transactional
	public R deleteTrue(Long id) {
		if (id == null) {
			return R.error("ID不能为空");
		}
		
		ysGridButtonMapper.deleteTrue(id);
		
		// 删除角色-按钮
		sysMapper.deleteTrueYsRoleButtonByButton(id);
		
		return R.ok();
	}

	@Override
	public R add(Long gridId,
			Integer styleType,
			String name,
			String event,
			String icon,
			String nameShiroAuth,
			Integer sortNo,
			String remark) {
		if (gridId == null) {
			return R.error("所属子列表不能为空");
		}
		
		YsGridButton entity = YsGridButton.ysGridButton(gridId, styleType, name, event, icon, nameShiroAuth, sortNo, remark, 1);
		ysGridButtonMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}

	@Override
	public R edit(Map<String, Object> params) {

		YsGridButton entity = JSON.parseObject(JSON.toJSONString(params), YsGridButton.class);
		ysGridButtonMapper.update(entity);
		
		return R.ok(EditDto.closeAndReload());
	}
}
